package com.demo.crm.service;

import com.arvato.crm.common.DateUtils;
import com.arvato.crm.model.FilterRule;
import com.arvato.crm.model.PagingInfo;
import com.arvato.crm.repository.NamedQueryTools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by tom on 2016-12-29.
 */

@Service
public class OrderSummaryServices {

    public List<Map<String,Object>> queryorderSummaryReportByMonthy(final Map<String,Object>params){
    	if(StringUtils.isNotBlank((String)params.get("startMonthy"))){
    		params.put("startMonthy", params.get("startMonthy")+"-01");
    	} if(StringUtils.isNotBlank((String)params.get("endMonthy"))){
    		params.put("startMonthy", params.get("endMonthy")+"-01");
    	}

        return NamedQueryTools.queryForMap(
                NamedQueryTools.createDynamicSqlQuery("queryorderSummaryReport",params)

        );
    }
    public Map<String,Object> queryorderSummaryPaging(final PagingInfo pagingInfo){

    	 final Query queryTotal=NamedQueryTools.createDynamicSqlQuery("queryorderSummaryReportTatal", new HashMap<String, Object>() {
             {
                 List<FilterRule> rules=pagingInfo.getRules();
                 if(CollectionUtils.isNotEmpty(rules)){
                     for(FilterRule rule:rules){
                    	 if(rule.getField().equals("startMonthy")||rule.getField().equals("endMonthy")&&StringUtils.isNotBlank(rule.getValue().toString())){
                    	      this.put(rule.getField(),rule.getValue()+"-01");
                    	 }else{
                    	      this.put(rule.getField(),rule.getValue());
                    	 }
                   
                     }
                 }
             }
         });


        List<FilterRule> rules=pagingInfo.getRules();
        Map<String,Object> params=new HashMap<String,Object>();
        String startMonthy="";
        String endMonthy="";
        if(CollectionUtils.isNotEmpty(rules)){
            for(FilterRule rule:rules){
                if (StringUtils.isNotBlank(rule.getValue().toString())){
       
                    params.put(rule.getField(),rule.getValue());
                  
                   // 获取输入日期
                    if(rule.getField().equals("startMonthy")){
                    	params.put(rule.getField(),rule.getValue()+"-01");
                        startMonthy=(String) rule.getValue();
                    } if(rule.getField().equals("endMonthy")){
                        endMonthy=(String) rule.getValue();
                        params.put(rule.getField(),rule.getValue()+"-01");
                    }

                }
            }
        }
        return getHapMap(params,pagingInfo,queryTotal);
    }
    
    public Map<String,Object> getHapMap(Map<String,Object> params,PagingInfo pagingInfo,Query queryTotal){
    	Map<String,Object> map=new HashMap<String,Object>();
		params.put("firstNum", (pagingInfo.getPage()-1)*pagingInfo.getRows());
		params.put("endNum", pagingInfo.getPage()*pagingInfo.getRows());
		Integer.parseInt(queryTotal.getSingleResult().toString());
		map.put("total",Integer.parseInt(queryTotal.getSingleResult().toString()));
		map.put("rows",NamedQueryTools.queryForMap(
        NamedQueryTools.createDynamicSqlQuery("queryorderSummaryReport",params)));
		return map;

    } 

}
